package org.jash.shop2212;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopActivity extends AppCompatActivity {
    private GridView lv;
    private GoodsAdapter adapter;
    private Spinner sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        lv = findViewById(R.id.lv);
        sp = findViewById(R.id.sp);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"电脑", "手机"});
        sp.setAdapter(adapter1);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ShopActivity.this, ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter = new GoodsAdapter();
        lv.setAdapter(adapter);

        HttpUtils.doGet("http://10.161.9.80:7003/goods/getAllGoods", new HashMap<String, Object>() {{
                    // 写入参数
            put("goods_category", 0);
                }}, GoodsEntity.class,
                r -> {
                    // 正确的处理
                    if (r.getErrorCode() == 200) {
                        adapter.addAll(r.getData());
                    } else {
                        Toast.makeText(this, r.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                }, t -> {
                    // 错误的处理
                    Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                });
        lv.setOnItemClickListener((parent, view, position, id) -> {
            GoodsEntity.DataDTO dto = adapter.getItem(position);
            Toast.makeText(this, "点击了：" + dto.getGoodsDesc(), Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, DetailActivity.class).putExtra("a", dto));
        });
        findViewById(R.id.nav_3).setOnClickListener(v -> {
            startActivity(new Intent(this, CartActivity.class));
        });
//        lv.setOnItemLongClickListener((parent, view, position, id) -> {
//            GoodsEntity.DataDTO dto = adapter.getItem(position);
//            Toast.makeText(this, "价格：" + dto.getGoodsPrice(), Toast.LENGTH_SHORT).show();
//            return false;
//        });
    }
}